package com.dingdang.sms.allinone.sender.impl

import com.dingdang.sms.allinone.config.AllInOneConfigurationProperties
import com.dingdang.sms.allinone.sender.AllInOneSender
import com.dingdang.sms.allinone.sender.ResultWrapper
import com.dingdang.sms.allinone.sender.Target
import com.jdcloud.sdk.service.sms.client.SmsClient
import com.jdcloud.sdk.service.sms.model.BatchSendRequest

class JdCloudSender(val properties: AllInOneConfigurationProperties) : AllInOneSender {

    val smsClient by lazy { SmsClient.builder().build() }
    override fun send(target: Target, block: (ResultWrapper) -> Unit) {
        val result = send(target)
        block(result)
    }

    override fun send(target: Target): ResultWrapper {
        target.checkTemplateId()
        target.checkMobileList()
        val request = BatchSendRequest()
        request.templateId = target.templateId
        request.phoneList = target.mobileList
        request.params = target.params?.values?.toList()
        request.signId = properties.jdCloud?.signId
        request.regionId = properties.jdCloud?.regionId
        val response = smsClient.batchSend(request)
        return if (response.result.code == "200") {
            ResultWrapper().ok(response.result.message, response.result.data)
        } else {
            ResultWrapper().error(response.result.message, response.result.code.toInt(), response.result.data)
        }
    }
}